using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Text;

using CSP.SimpleCMS.Data;
using CSP.MembershipInterface.ObjectModel;

namespace CSP.SimpleCMS.DataModel
{
    /// <summary>Manages the operations related with a Citizen</summary>
    public static class CitizenDM
    {
        /// <summary>Returns an array with all the Citizens stored in the data source</summary>
        /// <returns>An array of Citizens</returns>         
        public static Citizen[] GetCitizens()
        {
            List<Citizen> citizens = new List<Citizen>();

            DataSet dataSet = new DataSet();
            using (DataAccess dataAccess = new DataAccess())
            {
                dataSet = dataAccess.ExecSPReturnDS(DMConstants.SPCitizenGET, null);
            }

            if (dataSet != null && dataSet.Tables.Count == 1 && dataSet.Tables[0].Rows.Count > 0)
            {
                foreach (DataRow row in dataSet.Tables[0].Rows)
                {
                    Citizen citizen = ParseRow(row);
                    citizens.Add(citizen);
                }
            }

            return citizens.ToArray();
        }

        /// <summary>Returns a Citizen with the username sent by parameter</summary>
        /// <param name="username">The username which identifies the Citizen to be found</param>
        /// <returns>A Citizen object</returns>            
        public static Citizen GetCitizenByUsername(string username)
        {
            Citizen citizen = null;

            DataSet dataSet = new DataSet();
            using (DataAccess dataAccess = new DataAccess())
            {
                dataSet = dataAccess.ExecSPReturnDS(DMConstants.SPCitizenGET, DMConstants.FieldUsername, username);
            }

            if (dataSet != null && dataSet.Tables.Count == 1 && dataSet.Tables[0].Rows.Count == 1)
            {
                citizen = ParseRow(dataSet.Tables[0].Rows[0]);
            }

            return citizen;
        }

        /// <summary>Returns an or a null value depending on the value of the object sent by param</summary>
        /// <param name="caseType">The object to be evaluated</param>
        /// <returns>The object or null value</returns>    
        private static object IsNullOrEmpty(object o)
        {
            if (o == null || (o is string && ((string)o).Length == 0))
                return System.DBNull.Value;
            else
                return o;
        }

        /// <summary>Parses a datarow to a CaseType</summary>
        /// <param name="row">The datarow to be parsed</param>
        /// <returns>A CaseType object with the information stored in the datarow</returns>                  
        private static Citizen ParseRow(DataRow row)
        {
            Citizen citizen = new Citizen();

            citizen.Username = row[DMConstants.FieldUsername].ToString();
            citizen.FirstName = row[DMConstants.FieldFirstName].ToString();
            citizen.LastName = row[DMConstants.FieldLastName].ToString();
            citizen.DisplayName = row[DMConstants.FieldDisplayName].ToString();
            citizen.EmailAddress = row[DMConstants.FieldEmailAddress].ToString();
            citizen.DefaultGroup = row[DMConstants.FieldDefaultGroup].ToString();
            citizen.Address = row[DMConstants.FieldAddress].ToString();
            citizen.City = row[DMConstants.FieldCity].ToString();
            citizen.PostalCode = row[DMConstants.FieldPostalCode].ToString();
            if (row[DMConstants.FieldPhone] != System.DBNull.Value)
                citizen.Phone = row[DMConstants.FieldPhone].ToString();
            citizen.Children = row[DMConstants.FieldChildren].ToString();
            citizen.ElderlyRelatives = row[DMConstants.FieldElderlyRelatives].ToString();
            if (row[DMConstants.FieldPhotoUrl] != System.DBNull.Value)
                citizen.PhotoUrl = row[DMConstants.FieldPhotoUrl].ToString();
            citizen.LastModified = Convert.ToDateTime(row[DMConstants.FieldLastModified]);
            citizen.CreatedDate = Convert.ToDateTime(row[DMConstants.FieldCreatedDate]);

            return citizen;
        }


    }
}
